<?php
namespace Home\Controller;
use Think\Controller;
class CoinController extends Controller {

	public function _initialize()
    {
        //未登录、登录超时、长时间未动
    	if (empty($_SESSION['uid']) || time() > $_SESSION['overtime'] || empty($_SESSION['overtime']) ) {

    		$this->redirect('Common/login');
    	}

        session('overtime',(time()+60*60*C('overtime')));

        
        
    }


    /**
     * [transform 积分转换]
     * @return [type] [description]
     */
    public function transform(){
        $score_center = 123.45;//中心积分
        $score_register = 1;//注册积分
        $score_dynamic = 0.85;// 动态积分

        $this->assign('score_center',$score_center);
        $this->assign('score_register',$score_register);
        $this->assign('score_dynamic',$score_dynamic);
        $this->display();
    }


    /**
     * [transfer 积分转账]
     * @return [type] [description]
     */
    public function transfer(){
        $uid = $_SESSION['uid'];
        
        $m_user = M('User');
        $where['id'] = $uid;

        $userInfo = $m_user->where($where)->find();

        $poundage = C('cenToTra');//手续费


        $this->assign('poundage',$poundage);
        $this->assign('userInfo',$userInfo);
        $this->display();
    }


    /**
     * [ctrl_cenToTra 中心积分转交易积分]
     * @return [type] [description]
     */
    public function ctrl_cenToTra(){
        if(IS_AJAX){
            $uid = $_SESSION['uid'];
            $change_center = trim($_REQUEST['cenToTra']);//转化积分
            $poundage = trim($_REQUEST['poundage']);//手续费费率
            if (empty($change_center)) {
                $this->ajaxReturn(array(
                    'code'=>0,
                    'msg' => '转化积分不能为空'
                ));
            }else{
                if (empty($poundage)) {
                    $poundage = C('cenToTra');//手续费
                }

                

                $m_user = M('User');
                $where_user['id'] = $uid;

                $userInfo = $m_user->where($where_user)->find();

                $old_center = $userInfo['score_center'];
                $new_center = $old_center - $change_center;

                if ($change_center > $old_center) {
                    $this->ajaxReturn(array(
                        'code'=>0,
                        'msg' => '中心积分不足，无法转化'
                    ));
                }else{
                    $old_trading = $userInfo['score_trading'];
                    $change_trading = $change_center * ((float)(100 - $poundage) / 100.00);
                    $new_trading = $old_trading + $change_trading;

                    $save['score_center'] = $new_center;
                    $save['score_trading'] = $new_trading;

                    $res = $m_user->where($where_user)->save($save);
                    if ($res) {
                        //添加中心积分记录
                        addCenterTable($userInfo['id'],$old_center,$change_center,$new_center,'转交易积分扣除'.$change_center,2,$poundage);
                        // 添加交易积分记录
                        addTradingTable($userInfo['id'],$old_trading,$change_trading,$new_trading,'中心积分转化，新增'.$change_trading.'，已扣除手续费');
                        

                        $this->ajaxReturn(array(
                            'code'=>1,
                            'msg' => '转化成功',
                            'trading'=> $change_trading
                        ));
                    }else{
                        $this->ajaxReturn(array(
                            'code'=>0,
                            'msg' => '转化失败'
                        ));
                    }
                }

            }
        }else{
            $this->redirect('Common/BadRequest');
        }
    }


    /**
     * [ctrl_TraToCen 交易积分转中心积分]
     * @return [type] [description]
     */
    public function ctrl_TraToCen(){
        if(IS_AJAX){
            $uid = $_SESSION['uid'];
            $change_trading = trim($_REQUEST['traToCen']);//转化积分

            if (empty($change_trading)) {
                $this->ajaxReturn(array(
                    'code'=>0,
                    'msg' => '转化积分不能为空'
                ));
            }else{
                $m_user = M('User');
                $where_user['id'] = $uid;

                $userInfo = $m_user->where($where_user)->find();

                $old_trading = $userInfo['score_trading'];
                $new_trading = $old_trading - $change_trading;

                if ($change_trading > $old_trading) {
                    $this->ajaxReturn(array(
                        'code'=>0,
                        'msg' => '交易积分不足，无法转化'
                    ));
                }else{
                    $old_center = $userInfo['score_center'];
                    $change_center = $change_trading;
                    $new_center = $old_center + $change_center;

                    $save['score_trading'] = $new_trading;
                    $save['score_center'] = $new_center;

                    $res = $m_user->where($where_user)->save($save);
                    if ($res) {
                        // 添加交易积分记录
                        addTradingTable($userInfo['id'],$old_trading,$change_trading,$new_trading,'转中心积分扣除'.$change_trading,2);
                        //添加中心积分记录
                        addCenterTable($userInfo['id'],$old_center,$change_center,$new_center,'交易积分转化，新增'.$change_center);

                        $this->ajaxReturn(array(
                            'code'=>1,
                            'msg' => '转化成功'
                        ));
                    }else{
                        $this->ajaxReturn(array(
                            'code'=>0,
                            'msg' => '转化失败'
                        ));
                    }
                }
            }
        }else{
            $this->redirect('Common/BadRequest');
        }
    }


    /**
     * [ctrl_CenToTask 中心积分转任务积分]
     * @return [type] [description]
     */
    public function ctrl_CenToTask(){
        if(IS_AJAX){

            $uid = $_SESSION['uid'];
            $change_center = trim($_REQUEST['cenToTask']);//转化积分

            if (empty($change_center)) {
                $this->ajaxReturn(array(
                    'code'=>0,
                    'msg' => '转化积分不能为空'
                ));
            }else{

                $m_user = M('User');
                $where_user['id'] = $uid;

                $userInfo = $m_user->where($where_user)->find();

                $old_center = $userInfo['score_center'];
                $new_center = $old_center - $change_center;

                if ($change_center > $old_center) {
                    $this->ajaxReturn(array(
                        'code'=>0,
                        'msg' => '中心积分不足，无法转化'
                    ));
                }else{
                    $old_task = $userInfo['score_task'];
                    $change_task = $change_center;
                    $new_task = $old_task + $change_task;

                    $save['score_center'] = $new_center;
                    $save['score_task'] = $new_task;
                    $res = $m_user->where($where_user)->save($save);
                    if ($res) {
                        //添加中心积分记录
                        addCenterTable($userInfo['id'],$old_center,$change_center,$new_center,'转任务积分扣除'.$change_center,2);
                        // 添加任务积分记录
                        addTaskTable($userInfo['id'],$old_task,$change_task,$new_task,'中心积分转化，新增'.$change_task);

                        $this->ajaxReturn(array(
                            'code'=>1,
                            'msg' => '转化成功'
                        ));
                    }else{
                        $this->ajaxReturn(array(
                            'code'=>0,
                            'msg' => '转化失败'
                        ));
                    }
                }
            }
        }else{
            $this->redirect('Common/BadRequest');
        }
    }


    /**
     * [taskapply 任务申请]
     * @return [type] [description]
     */
    public function taskapply(){
        $uid = $_SESSION['uid'];
        

        $m_applytask = M('Applytask');

        $where['user_id'] = $uid;
        $count = $m_applytask->where($where)->count();
        $Page  = new \Think\Page($count,20);
        $show  = $Page->show();

        $applytaskList = $m_applytask->where($where)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign('page',$show);
        $this->assign('applytaskList',$applytaskList);
        $this->display();
    }


    /**
     * [returncenter 转回中心]
     * @return [type] [description]
     */
    public function returncenter(){
        $score_center = 123.45;//中心积分
        $score_betting = 880.20;//投注积分

        $this->assign('score_center',$score_center);
        $this->assign('score_betting',$score_betting);
        $this->display();
    }


    /**
     * [tokeytask 动态秘钥转任务秘钥]
     * @return [type] [description]
     */
    public function tokeytask(){
        $uid = $_SESSION['uid'];
        
        $m_user = M('User');
        $where['id'] = $uid;

        $userInfo = $m_user->where($where)->find();

        $this->assign('userInfo',$userInfo);
        $this->display();
    }


    /**
     * [ctrl_keytask 动态秘钥转任务秘钥]
     * @return [type] [description]
     */
    public function ctrl_keytask(){
        if (IS_AJAX) {
            $uid = $_SESSION['uid'];
            $num = trim($_REQUEST['num']);

            $m_user = M("User");

            $where_user['id'] = $uid;
            $userInfo = $m_user->where($where_user)->find();
            if ($userInfo['key_dynamic'] < $num) {
                $this->ajaxReturn(array(
                    'code'=>0,
                    'msg' => '动态秘钥不足，无法转换'
                ));
            }else{
                $old_key_dynamic = $userInfo['key_dynamic'];
                $change_key_dynamic = $num;
                $new_key_dynamic = $old_key_dynamic - $change_key_dynamic;

                $old_key_task = $userInfo['key_task'];
                $change_key_task = $num;
                $new_key_task = $old_key_task + $change_key_task;

                $save['key_dynamic'] = $new_key_dynamic;
                $save['key_task'] = $new_key_task;

                $res = $m_user->where($where_user)->save($save);
                if ($res) {
                    // 添加动态秘钥记录、添加任务秘钥记录
                    addKeyDynamicTable($userInfo['id'],$old_key_dynamic,$change_key_dynamic,$new_key_dynamic,'转任务积分扣除'.$change_key_dynamic,2);
                    addKeyTaskTable($userInfo['id'],$old_key_task,$change_key_task,$new_key_task,'动态积分转换，新增'.$change_key_task);
                    $this->ajaxReturn(array(
                        'code'=>1,
                        'msg' => '转换成功'
                    ));
                }else{
                    $this->ajaxReturn(array(
                        'code'=>0,
                        'msg' => '转换失败'
                    ));
                }
            }
        }else{
            $this->redirect('Common/BadRequest');
        }
    }

}